{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 本地模型列表\n",
    "qwen2-7b-instruct:latest families=['qwen2'], parameter_size='7.6B', quantization_level='Q5_K_M')), \n",
    "\n",
    "qwen2.5:14b  families=['qwen2'], parameter_size='14.8B',  quantization_level='Q4_K_M'))])\n",
    "\n",
    "llava:13b families=['llama', 'clip'], parameter_size='13B', quantization_level='Q4_0')),\n",
    "\n",
    "\n",
    "bge-m3:latest   families=['bert'], parameter_size='566.70M', quantization_level='F16')), \n",
    "\n",
    "bge-large:latest     families=['bert'], parameter_size='334.09M', quantization_level='F16')),\n",
    "\n",
    "\n",
    "bge-reranker-v2-m3:latest   families=['bert'], parameter_size='567.75M', quantization_level='F16')), \n",
    "\n",
    "bge-large-zh:latest     families=['bert'], parameter_size='324.47M', quantization_level='F16')),\n",
    "\n",
    "\n",
    "zyw0605688/bge-reranker-v2-m3:latest     families=['bert'], parameter_size='567.75M', quantization_level='F16')), "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Model(model='bge-m3:latest', modified_at=datetime.datetime(2025, 3, 12, 9, 25, 11, tzinfo=TzInfo(+08:00)), digest='7907646426070047a77226ac3e684fbbe8410524f7b4a74d02837e43f2146bab', size=1157672605, details=ModelDetails(parent_model='', format='gguf', family='bert', families=['bert'], parameter_size='566.70M', quantization_level='F16')),\n",
      " Model(model='deepseek-r1:32b', modified_at=datetime.datetime(2025, 2, 8, 16, 43, 39, tzinfo=TzInfo(+08:00)), digest='38056bbcbb2d068501ecb2d5ea9cea9dd4847465f1ab88c4d4a412a9f7792717', size=19851337640, details=ModelDetails(parent_model='', format='gguf', family='qwen2', families=['qwen2'], parameter_size='32.8B', quantization_level='Q4_K_M')),\n",
      " Model(model='qwen2.5:14b', modified_at=datetime.datetime(2025, 1, 13, 16, 41, 37, tzinfo=TzInfo(+08:00)), digest='fa236d5d698ce720bc238eedc16287ae2ccca5da83003d1e6039476912aee59c', size=8988124069, details=ModelDetails(parent_model='', format='gguf', family='qwen2', families=['qwen2'], parameter_size='14.8B', quantization_level='Q4_K_M'))]\n"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "client = ollama.Client(host='http://192.168.20.43:11434')\n",
    "result = client.list()\n",
    "\n",
    "from pprint import pprint\n",
    "pprint(result.models)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 格式化client.list()\n",
    "\n",
    "将client.list()的输出格式化，更易读。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    {\n",
      "        \"model\": \"bge-m3:newest\",\n",
      "        \"families\": [\n",
      "            \"bert\"\n",
      "        ],\n",
      "        \"parameter_size\": \"566.70M\",\n",
      "        \"quantization_level\": \"F16\"\n",
      "    },\n",
      "    {\n",
      "        \"model\": \"deepseek-r1:14b\",\n",
      "        \"families\": [\n",
      "            \"qwen2\"\n",
      "        ],\n",
      "        \"parameter_size\": \"14.8B\",\n",
      "        \"quantization_level\": \"Q4_K_M\"\n",
      "    },\n",
      "    {\n",
      "        \"model\": \"deepseek-r1:8b\",\n",
      "        \"families\": [\n",
      "            \"llama\"\n",
      "        ],\n",
      "        \"parameter_size\": \"8.0B\",\n",
      "        \"quantization_level\": \"Q4_K_M\"\n",
      "    },\n",
      "    {\n",
      "        \"model\": \"qwen2.5:14b\",\n",
      "        \"families\": [\n",
      "            \"qwen2\"\n",
      "        ],\n",
      "        \"parameter_size\": \"14.8B\",\n",
      "        \"quantization_level\": \"Q4_K_M\"\n",
      "    },\n",
      "    {\n",
      "        \"model\": \"bge-reranker-v2-m3:latest\",\n",
      "        \"families\": [\n",
      "            \"bert\"\n",
      "        ],\n",
      "        \"parameter_size\": \"567.75M\",\n",
      "        \"quantization_level\": \"F16\"\n",
      "    },\n",
      "    {\n",
      "        \"model\": \"qwen2-7b-instruct:latest\",\n",
      "        \"families\": [\n",
      "            \"qwen2\"\n",
      "        ],\n",
      "        \"parameter_size\": \"7.6B\",\n",
      "        \"quantization_level\": \"Q5_K_M\"\n",
      "    },\n",
      "    {\n",
      "        \"model\": \"llava:13b\",\n",
      "        \"families\": [\n",
      "            \"llama\",\n",
      "            \"clip\"\n",
      "        ],\n",
      "        \"parameter_size\": \"13B\",\n",
      "        \"quantization_level\": \"Q4_0\"\n",
      "    }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "# 提取核心信息并转换为JSON格式\n",
    "def extract_core_info(response):\n",
    "    core_info = [\n",
    "        {\n",
    "            \"model\": model.model,\n",
    "            \"families\": model.details.families,\n",
    "            \"parameter_size\": model.details.parameter_size,\n",
    "            \"quantization_level\": model.details.quantization_level\n",
    "        }\n",
    "        for model in response.models\n",
    "    ]\n",
    "    return json.dumps(core_info, indent=4)\n",
    "\n",
    "# 测试\n",
    "output_format = extract_core_info(client.list())\n",
    "print(output_format)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 显示模型信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'$defs': {'ModelDetails': {'properties': {'parent_model': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Parent Model'}, 'format': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Format'}, 'family': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Family'}, 'families': {'anyOf': [{'items': {'type': 'string'}, 'type': 'array'}, {'type': 'null'}], 'default': None, 'title': 'Families'}, 'parameter_size': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Parameter Size'}, 'quantization_level': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Quantization Level'}}, 'title': 'ModelDetails', 'type': 'object'}}, 'properties': {'modified_at': {'anyOf': [{'format': 'date-time', 'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Modified At'}, 'template': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Template'}, 'modelfile': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Modelfile'}, 'license': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'License'}, 'details': {'anyOf': [{'$ref': '#/$defs/ModelDetails'}, {'type': 'null'}], 'default': None}, 'model_info': {'anyOf': [{'type': 'object'}, {'type': 'null'}], 'title': 'Model Info'}, 'parameters': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Parameters'}}, 'required': ['model_info'], 'title': 'ShowResponse', 'type': 'object'}\n"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "client = ollama.Client(host='http://192.168.20.43:11434')\n",
    "\n",
    "# 显示模型信息\n",
    "print(client.show('qwen2.5:14b').model_json_schema())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
